home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / texinfo / info / info.c < prev    next >
C/C++ Source or Header  |  1994-01-28  |  15KB  |  512 lines

  1. /* info.c -- Display nodes of Info files in multiple windows. */
  2.  
  3. /* This file is part of GNU Info, a program for reading online documentation
  4.    stored in Info format.
  5.  
  6.    Copyright (C) 1993 Free Software Foundation, Inc.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2, or (at your option)
  11.    any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22.    Written by Brian Fox (bfox@ai.mit.edu). */
  23.  
  24. #include "info.h"
  25. #include "dribble.h"
  26. #include "getopt.h"
  27.  
  28. /* The version numbers of this version of Info. */
  29. int info_major_version = 2;
  30. int info_minor_version = 10;
  31. int info_patch_level = 1;
  32.  
  33. /* Non-zero means search all indices for APROPOS_SEARCH_STRING. */
  34. static int apropos_p = 0;
  35.  
  36. /* Variable containing the string to search for when apropos_p is non-zero. */
  37. static char *apropos_search_string = (char *)NULL;
  38.  
  39. /* Non-zero means print version info only. */
  40. static int print_version_p = 0;
  41.  
  42. /* Non-zero means print a short description of the options. */
  43. static int print_help_p = 0;
  44.  
  45. /* Array of the names of nodes that the user specified with "--node" on the
  46.    command line. */
  47. static char **user_nodenames = (char **)NULL;
  48. static int user_nodenames_index = 0;
  49. static int user_nodenames_slots = 0;
  50.  
  51. /* String specifying the first file to load.  This string can only be set
  52.    by the user specifying "--file" on the command line. */
  53. static char *user_filename = (char *)NULL;
  54.  
  55. /* String specifying the name of the file to dump nodes to.  This value is
  56.    filled if the user speficies "--output" on the command line. */
  57. static char *user_output_filename = (char *)NULL;
  58.  
  59. /* Non-zero indicates that when "--output" is specified, all of the menu
  60.    items of the specified nodes (and their subnodes as well) should be
  61.    dumped in the order encountered.  This basically can print a book. */
  62. int dump_subnodes = 0;
  63.  
  64. /* Structure describing the options that Info accepts.  We pass this structure
  65.    to getopt_long ().  If you add or otherwise change this structure, you must
  66.    also change the string which follows it. */
  67. #define APROPOS_OPTION 1
  68. #define DRIBBLE_OPTION 2
  69. #define RESTORE_OPTION 3
  70. static struct option long_options[] = {
  71.   { "apropos", 1, 0, APROPOS_OPTION },
  72.   { "directory", 1, 0, 'd' },
  73.   { "node", 1, 0, 'n' },
  74.   { "file", 1, 0, 'f' },
  75.   { "subnodes", 0, &dump_subnodes, 1 },
  76.   { "output", 1, 0, 'o' },
  77.   { "help", 0, &print_help_p, 1 },
  78.   { "version", 0, &print_version_p, 1 },
  79.   { "dribble", 1, 0, DRIBBLE_OPTION },
  80.   { "restore", 1, 0, RESTORE_OPTION },
  81.   {NULL, 0, NULL, 0}
  82. };
  83.  
  84. /* String describing the shorthand versions of the long options found above. */
  85. static char *short_options = "d:n:f:o:s";
  86.  
  87. /* When non-zero, the Info window system has been initialized. */
  88. int info_windows_initialized_p = 0;
  89.  
  90. /* Some "forward" declarations. */
  91. static void usage (), info_short_help (), remember_info_program_name ();
  92.  
  93.  
  94. /* **************************************************************** */
  95. /*                                    */
  96. /*          Main Entry Point to the Info Program            */
  97. /*                                    */
  98. /* **************************************************************** */
  99.  
  100. int
  101. main (argc, argv)
  102.      int argc;
  103.      char **argv;
  104. {
  105.   int getopt_long_index;    /* Index returned by getopt_long (). */
  106.   NODE *initial_node;        /* First node loaded by Info. */
  107.  
  108. #if defined (NeXT) && defined (NOTDEF)
  109.   malloc_debug (0x0ffffffff);
  110. #endif /* NeXT && NOTDEF */
  111.  
  112.   remember_info_program_name (argv[0]);
  113.  
  114.   while (1)
  115.     {
  116.       int option_character;
  117.  
  118.       option_character = getopt_long
  119.     (argc, argv, short_options, long_options, &getopt_long_index);
  120.  
  121.       /* getopt_long () returns EOF when there are no more long options. */
  122.       if (option_character == EOF)
  123.     break;
  124.  
  125.       /* If this is a long option, then get the short version of it. */
  126.       if (option_character == 0 && long_options[getopt_long_index].flag == 0)
  127.     option_character = long_options[getopt_long_index].val;
  128.  
  129.       /* Case on the option that we have received. */
  130.       switch (option_character)
  131.     {
  132.     case 0:
  133.       break;
  134.  
  135.       /* User wants to add a directory. */
  136.     case 'd':
  137.       info_add_path (optarg, INFOPATH_PREPEND);
  138.       break;
  139.  
  140.       /* User is specifying a particular node. */
  141.     case 'n':
  142.       add_pointer_to_array (optarg, user_nodenames_index, user_nodenames,
  143.                 user_nodenames_slots, 10, char *);
  144.       break;
  145.  
  146.       /* User is specifying a particular Info file. */
  147.     case 'f':
  148.       if (user_filename)
  149.         free (user_filename);
  150.  
  151.       user_filename = savestring (optarg);
  152.       break;
  153.  
  154.       /* User is specifying the name of a file to output to. */
  155.     case 'o':
  156.       if (user_output_filename)
  157.         free (user_output_filename);
  158.       user_output_filename = savestring (optarg);
  159.       break;
  160.  
  161.       /* User is specifying that she wishes to dump the subnodes of
  162.          the node that she is dumping. */
  163.     case 's':
  164.       dump_subnodes = 1;
  165.       break;
  166.  
  167.       /* User has specified a string to search all indices for. */
  168.     case APROPOS_OPTION:
  169.       apropos_p = 1;
  170.       maybe_free (apropos_search_string);
  171.       apropos_search_string = savestring (optarg);
  172.       break;
  173.  
  174.       /* User has specified a dribble file to receive keystrokes. */
  175.     case DRIBBLE_OPTION:
  176.       close_dribble_file ();
  177.       open_dribble_file (optarg);
  178.       break;
  179.  
  180.       /* User has specified an alternate input stream. */
  181.     case RESTORE_OPTION:
  182.       info_set_input_from_file (optarg);
  183.       break;
  184.  
  185.     default:
  186.       usage ();
  187.     }
  188.     }
  189.  
  190.   /* If the user specified --version, then show the version and exit. */
  191.   if (print_version_p)
  192.     {
  193.       printf ("GNU Info, Version %s.\n", version_string ());
  194.       exit (0);
  195.     }
  196.  
  197.   /* If the `--help' option was present, show the help and exit. */
  198.   if (print_help_p)
  199.     {
  200.       info_short_help ();
  201.       exit (0);
  202.     }
  203.   
  204.   /* If the user hasn't specified a path for Info files, default that path
  205.      now. */
  206.   if (!infopath)
  207.     {
  208.       char *path_from_env, *getenv ();
  209.  
  210.       path_from_env = getenv ("INFOPATH");
  211.  
  212.       if (path_from_env)
  213.     info_add_path (path_from_env);
  214.       else
  215.     info_add_path (DEFAULT_INFOPATH);
  216.     }
  217.  
  218.   /* If the user specified a particular filename, add the path of that
  219.      file to the contents of INFOPATH. */
  220.   if (user_filename)
  221.     {
  222.       char *directory_name, *temp;
  223.  
  224.       directory_name = savestring (user_filename);
  225.       temp = filename_non_directory (directory_name);
  226.  
  227.       if (temp != directory_name)
  228.     {
  229.       *temp = 0;
  230.       info_add_path (directory_name, INFOPATH_PREPEND);
  231.     }
  232.  
  233.       free (directory_name);
  234.     }
  235.  
  236.   /* If the user wants to search every known index for a given string,
  237.      do that now, and report the results. */
  238.   if (apropos_p)
  239.     {
  240.       info_apropos (apropos_search_string);
  241.       exit (0);
  242.     }
  243.  
  244.   /* Get the initial Info node.  It is either "(dir)Top", or what the user
  245.      specifed with values in user_filename and user_nodenames. */
  246.   if (user_nodenames)
  247.     initial_node = info_get_node (user_filename, user_nodenames[0]);
  248.   else
  249.     initial_node = info_get_node (user_filename, (char *)NULL);
  250.  
  251.   /* If we couldn't get the initial node, this user is in trouble. */
  252.   if (!initial_node)
  253.     {
  254.       if (info_recent_file_error)
  255.     info_error (info_recent_file_error);
  256.       else
  257.     info_error
  258.       (CANT_FIND_NODE, user_nodenames ? user_nodenames[0] : "Top");
  259.       exit (1);
  260.     }
  261.  
  262.   /* Special cases for when the user specifies multiple nodes.  If we are
  263.      dumping to an output file, dump all of the nodes specified.  Otherwise,
  264.      attempt to create enough windows to handle the nodes that this user wants
  265.      displayed. */
  266.   if (user_nodenames_index > 1)
  267.     {
  268.       free (initial_node);
  269.  
  270.       if (user_output_filename)
  271.     dump_nodes_to_file
  272.       (user_filename, user_nodenames, user_output_filename, dump_subnodes);
  273.       else
  274.     begin_multiple_window_info_session (user_filename, user_nodenames);
  275.  
  276.       exit (0);
  277.     }
  278.  
  279.   /* If there are arguments remaining, they are the names of menu items
  280.      in sequential info files starting from the first one loaded.  That
  281.      file name is either "dir", or the contents of user_filename if one
  282.      was specified. */
  283.   while (optind != argc)
  284.     {
  285.       REFERENCE **menu;
  286.       REFERENCE *entry;
  287.       NODE *node;
  288.       char *arg;
  289.  
  290.       /* Remember the name of the menu entry we want. */
  291.       arg = argv[optind++];
  292.  
  293.       /* Build and return a list of the menu items in this node. */
  294.       menu = info_menu_of_node (initial_node);
  295.  
  296.       /* If there wasn't a menu item in this node, stop here, but let
  297.      the user continue to use Info.  Perhaps they wanted this node
  298.      and didn't realize it. */
  299.       if (!menu)
  300.     {
  301.       begin_info_session_with_error
  302.         (initial_node, "There is no menu in this node.");
  303.       exit (0);
  304.     }
  305.  
  306.       /* Find the specified menu item. */
  307.       entry = info_get_labeled_reference (arg, menu);
  308.  
  309.       /* If the item wasn't found, search the list sloppily.  Perhaps this
  310.      user typed "buffer" when they really meant "Buffers". */
  311.       if (!entry)
  312.     {
  313.       register int i;
  314.  
  315.       for (i = 0; entry = menu[i]; i++)
  316.         if (strnicmp (entry->label, arg, strlen (arg)) == 0)
  317.           break;
  318.     }
  319.  
  320.       /* If we failed to find the reference, start Info with the current
  321.      node anyway.  It is probably a misspelling. */
  322.       if (!entry)
  323.     {
  324.       char *error_message = "There is no menu item \"%s\" in this node.";
  325.  
  326.       info_free_references (menu);
  327.  
  328.       /* If we were supposed to dump this node, complain. */
  329.       if (user_output_filename)
  330.         info_error (error_message, arg);
  331.       else
  332.         begin_info_session_with_error (initial_node, error_message, arg);
  333.  
  334.       exit (0);
  335.     }
  336.  
  337.       /* We have found the reference that the user specified.  Clean it
  338.      up a little bit. */
  339.       if (!entry->filename)
  340.     {
  341.       if (initial_node->parent)
  342.         entry->filename = savestring (initial_node->parent);
  343.       else
  344.         entry->filename = savestring (initial_node->filename);
  345.     }
  346.  
  347.       /* Find this node.  If we can find it, then turn the initial_node
  348.      into this one.  If we cannot find it, try using the label of the
  349.      entry as a file (i.e., "(LABEL)Top").  Otherwise the Info file is
  350.      malformed in some way, and we will just use the current value of
  351.      initial node. */
  352.       node = info_get_node (entry->filename, entry->nodename);
  353.  
  354.       if (!node && entry->nodename &&
  355.       (strcmp (entry->label, entry->nodename) == 0))
  356.     node = info_get_node (entry->label, "Top");
  357.  
  358.       if (node)
  359.     {
  360.       free (initial_node);
  361.       initial_node = node;
  362.       info_free_references (menu);
  363.     }
  364.       else
  365.     {
  366.       char *temp = savestring (entry->label);
  367.       char *error_message;
  368.  
  369.       error_message = "Unable to find the node referenced by \"%s\".";
  370.  
  371.       info_free_references (menu);
  372.  
  373.       /* If we were trying to dump the node, then give up.  Otherwise,
  374.          start the session with an error message. */
  375.       if (user_output_filename)
  376.         info_error (error_message, temp);
  377.       else
  378.         begin_info_session_with_error (initial_node, error_message, temp);
  379.  
  380.       exit (0);
  381.     }
  382.     }
  383.  
  384.   /* If the user specified that this node should be output, then do that
  385.      now.  Otherwise, start the Info session with this node. */
  386.   if (user_output_filename)
  387.     dump_node_to_file (initial_node, user_output_filename, dump_subnodes);
  388.   else
  389.     begin_info_session (initial_node);
  390.  
  391.   exit (0);
  392. }
  393.  
  394. /* Return a string describing the current version of Info. */
  395. char *
  396. version_string ()
  397. {
  398.   static char *vstring = (char *)NULL;
  399.  
  400.   if (!vstring)
  401.     {
  402.       vstring = (char *)xmalloc (50);
  403.       sprintf (vstring, "%d.%d", info_major_version, info_minor_version);
  404.       if (info_patch_level)
  405.     sprintf (vstring + strlen (vstring), "-p%d", info_patch_level);
  406.     }
  407.   return (vstring);
  408. }
  409.  
  410. /* **************************************************************** */
  411. /*                                    */
  412. /*           Error Handling for Info                */
  413. /*                                    */
  414. /* **************************************************************** */
  415.  
  416. static char *program_name = (char *)NULL;
  417.  
  418. static void
  419. remember_info_program_name (fullpath)
  420.      char *fullpath;
  421. {
  422.   char *filename;
  423.  
  424.   filename = filename_non_directory (fullpath);
  425.   program_name = savestring (filename);
  426. }
  427.  
  428. /* Non-zero if an error has been signalled. */
  429. int info_error_was_printed = 0;
  430.  
  431. /* Non-zero means ring terminal bell on errors. */
  432. int info_error_rings_bell_p = 1;
  433.  
  434. /* Print FORMAT with ARG1 and ARG2.  If the window system was initialized,
  435.    then the message is printed in the echo area.  Otherwise, a message is
  436.    output to stderr. */
  437. void
  438. info_error (format, arg1, arg2)
  439.      char *format;
  440.      void *arg1, *arg2;
  441. {
  442.   info_error_was_printed = 1;
  443.  
  444.   if (!info_windows_initialized_p || display_inhibited)
  445.     {
  446.       fprintf (stderr, "%s: ", program_name);
  447.       fprintf (stderr, format, arg1, arg2);
  448.       fprintf (stderr, "\n");
  449.       fflush (stderr);
  450.     }
  451.   else
  452.     {
  453.       if (!echo_area_is_active)
  454.     {
  455.       if (info_error_rings_bell_p)
  456.         terminal_ring_bell ();
  457.       window_message_in_echo_area (format, arg1, arg2);
  458.     }
  459.       else
  460.     {
  461.       NODE *temp;
  462.  
  463.       temp = build_message_node (format, arg1, arg2);
  464.       if (info_error_rings_bell_p)
  465.         terminal_ring_bell ();
  466.       inform_in_echo_area (temp->contents);
  467.       free (temp->contents);
  468.       free (temp);
  469.     }
  470.     }
  471. }
  472.  
  473. /* Produce a very brief descripton of the available options and exit with
  474.    an error. */
  475. static void
  476. usage ()
  477. {
  478.   fprintf (stderr,"%s\n%s\n%s\n%s\n%s\n",
  479. "Usage: info [-d dir-path] [-f info-file] [-o output-file] [-n node-name]...",
  480. "            [--directory dir-path] [--file info-file] [--node node-name]...",
  481. "            [--help] [--output output-file] [--subnodes] [--version]",
  482. "            [--dribble dribble-file] [--restore from-file]",
  483. "            [menu-selection ...]");
  484.   exit (1);
  485. }
  486.  
  487. /* Produce a scaled down description of the available options to Info. */
  488. static void
  489. info_short_help ()
  490. {
  491.   printf ("%s", "\
  492. Here is a quick description of Info's options.  For a more complete\n\
  493. description of how to use Info, type `info info options'.\n\
  494. \n\
  495.    --directory DIR        Add DIR to INFOPATH.\n\
  496.    --file FILENAME        Specify Info file to visit.\n\
  497.    --node NODENAME        Specify nodes in first visited Info file.\n\
  498.    --output FILENAME        Output selected nodes to FILENAME.\n\
  499.    --dribble FILENAME        Remember user keystrokes in FILENAME.\n\
  500.    --restore FILENAME        Read initial keystrokes from FILENAME.\n\
  501.    --subnodes            Recursively output menu items.\n\
  502.    --help            Get this help message.\n\
  503.    --version            Display Info's version information.\n\
  504. \n\
  505. Remaining arguments to Info are treated as the names of menu\n\
  506. items in the initial node visited.  You can easily move to the\n\
  507. node of your choice by specifying the menu names which describe\n\
  508. the path to that node.  For example, `info emacs buffers'.\n");
  509.  
  510.   exit (0);
  511. }
  512.